package com.scwatch.core.web.back;

import com.scwatch.core.domain.Site;
import com.scwatch.core.domain.SiteComment;
import com.scwatch.core.service.OperationLogService;
import com.scwatch.core.service.SiteService;
import com.scwatch.core.support.Context;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

import static com.scwatch.core.constant.Constants.MESSAGE;
import static com.scwatch.core.constant.Constants.SAVE_SUCCESS;

/**
 * CommentConfController
 *
 * @author gjl
 */
@Controller
@RequestMapping("/core/comment_conf")
public class CommentConfController {
    private static final Logger logger = LoggerFactory
            .getLogger(CommentConfController.class);

    @RequiresPermissions("core:comment_conf:edit")
    @RequestMapping("edit.do")
    public String edit(HttpServletRequest request,
                       org.springframework.ui.Model modelMap) {
        Site site = Context.getCurrentSite();

        String infoid = request.getParameter("infoid");
        String ftype = request.getParameter("ftype");
        SiteComment siteComment = new SiteComment(site.getCustoms());
        modelMap.addAttribute("bean", siteComment);

        modelMap.addAttribute("infoid", infoid);
        modelMap.addAttribute("ftype", ftype);
        return "core/comment_conf/comment_conf";
    }

    @RequiresPermissions("core:comment_conf:update")
    @RequestMapping("update.do")
    public String update(SiteComment bean, HttpServletRequest request,
                         RedirectAttributes ra) {
        Site site = Context.getCurrentSite();
//		String infoid = request.getParameter("infoid");
//		String ftype =request.getParameter("ftype");
        siteService.updateConf(site, bean);
        logService.operation("opr.commentConf.edit", site.getName(),
                null, site.getId(), request);
        logger.info("update Comment config.");
        ra.addFlashAttribute(MESSAGE, SAVE_SUCCESS);

//		modelMap.addAttribute("infoid", infoid);
//		modelMap.addAttribute("ftype", ftype);
        return "redirect:edit.do";
    }

    @Autowired
    private OperationLogService logService;
    @Autowired
    private SiteService siteService;
}
